var navigateTo = (function () {
    let ifrObj;
    let transObj;

    function showPageTrans() {
        transObj.style.display = "block";
        ifrObj.style.transitionDuration = "0ms";
        ifrObj.style.opacity = 0;
        ifrObj.style.transform = "translateY(24px)";
    }

    function showPageTransDone() {
        transObj.style.display = "none";
        ifrObj.style.transitionDuration = "400ms";
        ifrObj.style.opacity = 1;
        ifrObj.style.transform = "translateY(0)";
    }

    function autoHeightFit(win) {
        let h;
        if (win.contentDocument && win.contentDocument.body.offsetHeight)
            h = win.contentDocument.body.offsetHeight;
        else if (win.Document && win.Document.body.scrollHeight)
            h = win.Document.body.scrollHeight;
        if (h < 320)
            h = 320;
        win.style.height = h + 64 + "px";
    }

    window.addEventListener("load", function () {
        ifrObj = document.getElementById("main-iframe");
        transObj = document.getElementById("transAnimation");
        window.setInterval(autoHeightFit, 3200, ifrObj);
        ifrObj.onload = function () {
            autoHeightFit(ifrObj);
            window.setTimeout(showPageTransDone, 400);
        };
        ifrObj.contentWindow.addEventListener("resize", function () {
            autoHeightFit(ifrObj);
        });
        if (ifrObj.src) {
            showPageTrans();
            window.setTimeout(showPageTransDone, 600);
        }
        let currentItem;
        //a标签的事件处理
        const links = document.querySelectorAll("#main-drawer a");
        links.forEach(a => {
            a.onclick = function (evt) {
                evt.preventDefault();
                if (this != currentItem) {
                    if (currentItem)
                        currentItem.classList.remove("mdui-list-item-active");
                    a.classList.add("mdui-list-item-active");
                    currentItem = this;
                    showPageTrans();
                    ifrObj.src = this.href;
                }
            };
        });

        //在a标签处理之前生成菜单
        // let roleId = sessionStorage.getItem('roleId');
        //let menuUrl = "../menu/emp_menu.html"
        // if (roleId == 2) {
        //     menuUrl = "../menu/admin_menu.html"
        // } else if (roleId == 3) {
        //     menuUrl = "../menu/charger_menu.html"
        // }
        // axios.get(menuUrl)
        //     .then(function (res) {
        //         // alert(res.data)
        //         // console.log(res.data)
        //         document.getElementById("main-drawer").innerHTML = res.data
        //
        //         mdui.mutation();//初始化组件
        //
        //         //a标签的事件处理
        //         const links = document.querySelectorAll("#main-drawer a");
        //         links.forEach(a => {
        //             a.onclick = function (evt) {
        //                 evt.preventDefault();
        //                 if (this != currentItem) {
        //                     if (currentItem)
        //                         currentItem.classList.remove("mdui-list-item-active");
        //                     a.classList.add("mdui-list-item-active");
        //                     currentItem = this;
        //                     showPageTrans();
        //                     ifrObj.src = this.href;
        //                 }
        //             };
        //         });
        //
        //     });

    });

    return (link) => {
        showPageTrans();
        ifrObj.src = link;
    }

})();
